<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>长投学堂</title>
</head>
<body>
  <div style="position:absolute;top:40%;left:50%">
    <img src="static/image/loading.gif" style="position:relative;right:50%;width:200px; height:200px;">
  </div>

  <script src="static/lib/jquery.min.js" type= "text/javascript"></script>

  <script>
      /**
       * 拉取微信主动授权
       */

      var server = 'https://m.ichangtou.com';
      var appid =  'wxe95fb252e5bc8152';

      var headers = {
          'X-iChangTou-Json-Api-Token': 'DE:_:w2qlJFV@ccOeiq41ENp><ETXh3o@aX8M<[_QOsZ<d8[Yz:NIMcKwpjtBk0e',
          'Content-Type': 'application/json; charset=utf-8'
      };

      $(document).ready(function(){

          if (window.location.href.indexOf('code=') > -1) {
              //处理code state
              var raw = window.location.search.split('code=')[1];
              var code = raw.split('&')[0];
              var state = window.location.search.split('state=')[1];
              //向服务器发送code
              sendWxCode(code,state);
          } else {
              goToOauth2();
          }
      });


      /**
       * 发送code
       * @param code
       */
      function sendWxCode(code, state){
          var data = JSON.stringify({code: code});
          //M站微信授权获取用户信息
          var url = server +'/wx/h5/base/ipo/openId';

          $.ajax( {
              url: url,
              headers: headers,
              data: data,
              type: 'post',
              dataType: 'json',
              success: function(wxAccount) {
                  //删除重试缓存
                  window.sessionStorage.removeItem('cache-zhouLe-reoauth-num');
                  if (wxAccount.openId) {
                      // 设置微信授权信息到缓存
                      window.sessionStorage.setItem('zhouLe', JSON.stringify(wxAccount));
                      //跳转回到App
                      goToApp();
                  } else if (wxAccount.up === '_sys_now'){
                      // 提醒正在升级]
                      alert('后台服务正在升级中..请稍后再试')
                  }
              },
              error: function(err) {
                  //授权失败，重复授权动作
                  var failtime = window.sessionStorage.getItem('cache-zhouLe-reoauth-num') ||  '0';

                  if(parseInt(failtime) == 3){
                      //删除重试缓存
                      window.sessionStorage.removeItem('cache-zhouLe-reoauth-num');
                      alert('抱歉, 网络开小差了, 点击重试');
                      goToOauth2();
                  }else{
                      window.sessionStorage.setItem('cache-zhouLe-reoauth-num', (parseInt(failtime) + 1) + '');
                      goToOauth2();
                  }
              }
          });
      }

      /**
       * 跳转至APP
       */
      function goToApp () {
        var preAppUrl =  window.sessionStorage.getItem('preAppUrl');
        window.sessionStorage.removeItem('preAppUrl');
        window.location.href = preAppUrl;
      }

      /**
       * 进行授权
       */
      function goToOauth2 () {
        var url = location.href.split('?')[0];

        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
                appid + '&redirect_uri=' +
                encodeURIComponent(url) +
                '&response_type=code&scope=snsapi_base&state=mapp' + '#wechat_redirect';
      }
  </script>
</body>
</html>
